package org.eiahe.hr.performance.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.utils.MapstructUtils;
import org.ehe.system.service.IHrEmployeeService;
import org.eiahe.hr.performance.domain.PerformancePushRecord;
import org.eiahe.hr.performance.domain.bo.PerformancePushRecordBo;
import org.eiahe.hr.performance.domain.vo.PerformancePushRecordVo;
import org.eiahe.hr.filler.EmployeeNameFiller;
import org.eiahe.hr.performance.mapper.PerformancePushRecordMapper;
import org.eiahe.hr.performance.service.IPerformancePushRecordService;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 绩效推送记录Service业务层处理
 *
 * @author zhangnn
 */
@RequiredArgsConstructor
@Service
public class PerformancePushRecordServiceImpl implements IPerformancePushRecordService {

    private final PerformancePushRecordMapper baseMapper;
    private final IHrEmployeeService employeeService;
    private final EmployeeNameFiller employeeNameFiller;

    @Override
    public PerformancePushRecordVo queryById(Long recordId) {
        PerformancePushRecordVo performancePushRecordVo = baseMapper.selectVoById(recordId);
        employeeNameFiller.fillEmployeeName(
            performancePushRecordVo,
            PerformancePushRecordVo::getEmployeeNo,
            PerformancePushRecordVo::setEmployeeName
        );
        return performancePushRecordVo;
    }


    private LambdaQueryWrapper<PerformancePushRecord> buildQueryWrapper(PerformancePushRecordBo record) {
        LambdaQueryWrapper<PerformancePushRecord> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(record.getEmployeeName())) {
            List<String> employeeNos = employeeService.queryByEmployeeName(record.getEmployeeName());
            if (CollUtil.isEmpty(employeeNos)) {
                lqw.eq(PerformancePushRecord::getEmployeeNo, -1);
            } else {
                lqw.in(PerformancePushRecord::getEmployeeNo, employeeNos);
            }
        }
        lqw.eq(StringUtils.isNotBlank(record.getEmployeeNo()), PerformancePushRecord::getEmployeeNo, record.getEmployeeNo());
        lqw.eq(StringUtils.isNotBlank(record.getPushStatus()), PerformancePushRecord::getPushStatus, record.getPushStatus());
        lqw.eq(StringUtils.isNotBlank(record.getReadStatus()), PerformancePushRecord::getReadStatus, record.getReadStatus());
        lqw.eq(StringUtils.isNotBlank(record.getDelFlag()), PerformancePushRecord::getDelFlag, record.getDelFlag());
        return lqw;
    }

    @Override
    public TableDataInfo<PerformancePushRecordVo> queryPageListVo(PerformancePushRecordBo record, PageQuery pageQuery) {
        LambdaQueryWrapper<PerformancePushRecord> lqw = buildQueryWrapper(record);
        Page<PerformancePushRecordVo> page = baseMapper.selectVoPage(pageQuery.build(), lqw);
        employeeNameFiller.fillEmployeeNames(
            page.getRecords(),
            PerformancePushRecordVo::getEmployeeNo,
            PerformancePushRecordVo::setEmployeeName
        );
        return TableDataInfo.build(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(PerformancePushRecordBo record) {
        PerformancePushRecord add = MapstructUtils.convert(record, PerformancePushRecord.class);
        return baseMapper.insert(add) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(PerformancePushRecordBo record) {
        PerformancePushRecord update = MapstructUtils.convert(record, PerformancePushRecord.class);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }
        if (isValid) {
            // 校验逻辑可以根据需要添加
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
